/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue.old;

import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.ui.SongAdapter;

/**
 * Handles all the UI in landscape mode. The landscape mode is where the user
 * selects songs to enqueue.

 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class PlayerUiLandscape {

  /**
   * All the songs are displayed when clicked (also default on opening).
   */
  private ImageButton allSongsButton;

  /**
   * Only starred songs are displayed when clicked.
   */
  private ImageButton starredSongsButton;

  /**
   * The actual list.
   */
  private ListView listView;

  /**
   * The adapter that holds the data of all the songs.
   */
  private SongAdapter allSongsAdapter;

  /**
   * The adapter that holds the data of the starred songs.
   */
  private SongAdapter starredSongsAdapter;

  /**
   * Reference needed to create new starred songs manager.
   */
  private MusicQueue musicQueue;

  /**
   * Drawable for the not selected Button.
   */
 // private Drawable drawable;
  
  /**
   * Drawable for the selected Button.
   */
 // private Drawable highlightedDrawable;
  
  /**
   * true if the starred items are shown.
   */
  private boolean showStarred = false;
  
  /**
   * The constructor sets the references.
   */
  public PlayerUiLandscape(MusicQueue musicQueue, ListView listView,
      SongAdapter allSongsAdapter, SongAdapter starredSongsAdapter,
      ImageButton allSongsButtonIn, ImageButton starredSongsButtonIn, Resources resources) {
    this.listView = listView;
    this.allSongsAdapter = allSongsAdapter;
    this.starredSongsAdapter = starredSongsAdapter;
    this.allSongsButton = allSongsButtonIn;
    this.starredSongsButton = starredSongsButtonIn;
    this.allSongsButton.setImageResource(R.drawable.allsongs);
    this.starredSongsButton.setImageResource(R.drawable.starred);
    this.musicQueue = musicQueue;
   // this.drawable = resources.getDrawable(R.drawable.newblackbutton_over);
   // this.highlightedDrawable = resources.getDrawable(R.drawable.newblackbutton2);

    listView.setFooterDividersEnabled(false);
    
    allSongsAdapter.notifyDataSetChanged();
    listView.setAdapter(allSongsAdapter);
    listView.setTextFilterEnabled(true);
    listView.setFastScrollEnabled(true);
  }

  /**
   * Initializes the UI.
   */
  public void initUi() {
    allSongsButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      	if (showStarred) {
  	     listView.setAdapter(allSongsAdapter);
  	    // allSongsButton.setBackgroundDrawable(highlightedDrawable);
  	    // starredSongsButton.setBackgroundDrawable(drawable);
         showStarred = false;
      	}
      }
    });
    starredSongsButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // have a better solution than always creating a new object for
        // the problem that starred songs can
        // change and that the adapter data needs to be updated.
      	if (!showStarred) {
      	// starredSongsAdapter = musicQueue.createNewStarredSongAdapter();
  	     listView.setAdapter(starredSongsAdapter);
  	    // allSongsButton.setBackgroundDrawable(drawable);
  	    // starredSongsButton.setBackgroundDrawable(highlightedDrawable);
  	     showStarred = true;
      	}
      }
    });
  }
  
  /**
   * Call this when the data set has changed to cause a refresh.
   */
  public void notifyDataSetChanged() {
    allSongsAdapter.notifyDataSetChanged();
    starredSongsAdapter.notifyDataSetChanged();
  }
}
